package com.mpale.soa.session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 读取Session值
 * 注销Session
 * @author lijian
 * @date 2020/6/15
 */
public class SessionReadServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
//        req.setCharacterEncoding("utf-8");
//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");

        HttpSession session = req.getSession();
        String id = session.getId();
        long accessedTime = session.getLastAccessedTime();
        // Session注销
//        session.invalidate();


        PrintWriter writer = resp.getWriter();
        writer.println("最后访问时间：" + accessedTime);
        writer.print("Session注销了，SessionId：" + id);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
